package pfc.pfcjoc.entities;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.World;

import pfc.pfcjoc.core.pfcjoc;
import playn.core.GroupLayer;

public abstract class PhysicElement extends Element {
  public PhysicElement(World world, GroupLayer worldLayer, Vec2 pos) {
    super(world, worldLayer, pos);
  }

  protected Body body;
  protected float prevX, prevY, prevA;


  public Body getBody() {
    return body;
  }


  public void update(float delta) {
    position = body.getPosition();

    prevX = body.getPosition().x;
    prevY = body.getPosition().y;
    prevA = body.getAngle();
  }


  public void setPosPhy(Vec2 pos) {
    body.setTransform(pos, body.getAngle());
    position = pos;

    prevX = body.getPosition().x;
    prevY = body.getPosition().y;
    prevA = body.getAngle();
  }


  public void paint(float alpha) {
    float x = (body.getPosition().x * alpha) + (prevX * (1f - alpha));
    float y = (body.getPosition().y * alpha) + (prevY * (1f - alpha));
    float a = (body.getAngle() * alpha) + (prevA * (1f - alpha));
    getLayer().setTranslation(pfcjoc.toPix(x), pfcjoc.toPix(y));
    getLayer().setRotation(a);
  }
}
